<?php


namespace App\backend\services;


use App\backend\models\GoodSkuModel;

class GoodSkuServices
{

    /**
     * 获取 - 商品回收站列表
     *
     * @param $data //请求参数数组
     * @return array
     */
    public static function getList($data)
    {
        $page = !isset($data['page']) ? 1 : $data['page'];unset($data['page']);
        $limit = !isset($data['limit']) ? 10 : $data['limit'];unset($data['limit']);

        $where_data = [];
        if (isset($data['sku_name']) && !empty($data['sku_name'])) {
            $where_data[] = ['sku_name','like',"%{$data['sku_name']}%"];
        }

        $list = GoodSkuModel::listed($page, $limit, $where_data);
        return $list;
    }

    /**
     * 添加/编辑 - 商品规格
     *
     * @param $data //请求参数数组
     * @return array
     */
    public static function goodSkuModify($data)
    {
        if (empty($data) && $data == false) {
            return [];
        }

        $ids['id'] = !empty($data['sku_id']) ? $data['sku_id'] : 0;unset($data['sku_id']);

        $list = '';
        $where_data = [];
        if (isset($ids['id']) && !empty($ids['id'])) {
            $data['updated_at'] = time();
            $where_data[] = ['id', '=', $ids['id']];
            GoodSkuModel::updatedData($ids, $data);
        } else {
            $data['created_at'] = time();
            $insert_id = GoodSkuModel::insertGetIdData($data);
            if ($insert_id) {
                $where_data[] = ['id', '=', $insert_id];
            }
        }

        if (!empty($where_data)) {
            $select_data = ['id', 'sku_name', 'sku_item', 'item_num', 'created_at'];
            $list = GoodSkuModel::info($where_data, $select_data);
            if (!empty($list->sku_item)) {
                $list->sku_item = get_string_array($list->sku_item);
            }
        }
        return $list;
    }

    /**
     * 删除 - 商品规格
     *
     * @param $data //请求参数数组
     * @return array
     */
    public static function goodSkuDelete($data)
    {
        if (empty($data) && $data == false) {
            return [];
        }

        $data['id'] = isset($data['sku_id']) && !empty($data['sku_id']) ? $data['sku_id'] : 0;unset($data['sku_id']);
        $list = GoodSkuModel::updatedData($data, ['status' => 1]);
        return $list;
    }

    /**
     * 详情 - 商品规格
     *
     * @param $data //请求参数数组
     * @return array
     */
    public static function goodsSkuInfo($data)
    {
        if (empty($data) && $data == false) {
            return [];
        }

        $select_data = ['id', 'sku_name', 'sku_item', 'item_num', 'created_at'];
        $data['id'] = isset($data['sku_id']) && !empty($data['sku_id']) ? $data['sku_id'] : 0;unset($data['sku_id']);
        $list = GoodSkuModel::info($data, $select_data);
        if (!empty($list)) {
            if (!empty($list->sku_item)) {
                $list->sku_item = get_string_array($list->sku_item);
            }
        }
        return $list;
    }

}
